Light Sensor API
This documentation page provides details about the Light Sensor API for Universal Windows Platform (UWP) applications.
Overview
The Light Sensor API allows your UWP applications to access ambient light data from devices. This can be useful for a variety of scenarios, such as automatically adjusting screen brightness, enhancing user experience in different lighting conditions, or implementing custom lighting-dependent features.
The light sensor reports the illuminance of the environment in lux.
Enabling the Light Sensor
Before you can use the light sensor, you need to declare its capability in your application's manifest file. Open your Package.appxmanifest file and add the following under the <Capabilities> element:
<DeviceCapability Name="proximity" />
Note: The light sensor is often grouped with the proximity sensor and requires the proximity device capability.
Accessing the Light Sensor
You can access the light sensor using the Windows.Devices.Sensors namespace. The primary class for interacting with the light sensor is LightSensor.
Instantiating the Light Sensor
You can get the default light sensor instance as follows:
var lightSensor = await Windows.Devices.Sensors.LightSensor.GetDefaultAsync();
It's important to check if the sensor is available on the device:
if (lightSensor == null)
{
System.Diagnostics.Debug.WriteLine("Light sensor not found.");
return;
}
Reading Sensor Data
You can read the current light sensor readings or set up event handlers to receive readings periodically.
Reading Current Data
To get the latest readings, call the GetCurrentReading() method:
var reading = lightSensor.GetCurrentReading();
The LightSensorReading object contains the illuminance value:
if (reading != null)
{
double illuminanceInLux = reading.IlluminanceInLux;
System.Diagnostics.Debug.WriteLine($"Current Illuminance: {illuminanceInLux} lux");
}
Handling Sensor Events
For continuous monitoring, you can subscribe to the ReadingChanged event. You can also control the desired report interval.
Setting the Report Interval
The ReportInterval property allows you to specify how frequently you want to receive sensor readings. Setting it to 0 requests the fastest possible interval.
lightSensor.ReportInterval = 1000; // Request readings every 1000ms (1 second)
If you don't set the interval, the system will use a default value.
Registering for Reading Changes
private LightSensor _lightSensor;
public MainPage()
{
this.InitializeComponent();
_lightSensor = await LightSensor.GetDefaultAsync();
if (_lightSensor != null)
{
uint minReportInterval = _lightSensor.MinimumReportInterval;
_lightSensor.ReportInterval = Math.Max(minReportInterval, 1000);
// Subscribe to the ReadingChanged event
_lightSensor.ReadingChanged += LightSensor_ReadingChanged;
}
}
private void LightSensor_ReadingChanged(LightSensor sender, LightSensorReadingChangedEventArgs args)
{
var reading = args.Reading;
double illuminanceInLux = reading.IlluminanceInLux;
// Update UI on the UI thread
Dispatcher.RunAsync(CoreDispatcherPriority.High, () =>
{
// Update your UI element here, e.g., a TextBlock
// illuminanceTextBlock.Text = $"{illuminanceInLux} lux";
System.Diagnostics.Debug.WriteLine($"Illuminance Update: {illuminanceInLux} lux");
});
}
Unregistering Event Handlers
It's crucial to unregister your event handlers when they are no longer needed to prevent memory leaks, especially when navigating away from a page.
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
if (_lightSensor != null)
{
// Unsubscribe from the ReadingChanged event
_lightSensor.ReadingChanged -= LightSensor_ReadingChanged;
_lightSensor = null;
}
base.OnNavigatedFrom(e);
}
Properties of LightSensor
| Property | Type | Description |
|---|---|---|
DeviceId |
string |
Gets the device identifier. |
MaxBatchSize |
uint |
Gets the maximum number of readings that can be batched. |
ReportInterval |
uint |
Gets or sets the desired report interval for sensor readings. |
MinimumReportInterval |
uint |
Gets the minimum report interval supported by the sensor. |
Properties of LightSensorReading
| Property | Type | Description |
|---|---|---|
IlluminanceInLux |
double |
Gets the illuminance value in lux. |
PerformanceCount |
ulong |
Gets the performance counter value. |
Timestamp |
Windows.Foundation.DateTime |
Gets the timestamp of the reading. |